//
//  TextView.m
//  group14
//
//  Created by lanou3g on 15/11/20.
//  Copyright © 2015年 吕志伟. All rights reserved.
//

#import "TextView.h"

@implementation TextView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 通知 监听文字的改变
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidchange) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//监听文字改变方法

- (void)textDidchange
{
    //重绘 (重新调用)
    
    [self setNeedsDisplay];
    
}

//画文字
- (void)drawRect:(CGRect)rect
{
    //重绘判断是否有文字  如果有文字  就直接返回 不画占位 文字
    
    if (self.hasText)return;
    
    //文字属性
    
    NSMutableDictionary * arrts = [NSMutableDictionary dictionary];
    
    arrts[NSFontAttributeName] = self.font;
    
    arrts[NSForegroundColorAttributeName] = self.placeholderColor ? self.placeholderColor:[UIColor grayColor];
    
    //画文字 画到矩形中
    
    CGRect placeholderRect = CGRectMake(5, 8, rect.size.width -10, rect.size.height - 16);
    
    [self.placeholder drawInRect:placeholderRect withAttributes:arrts];
}

- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = [placeholder copy];
    [self setNeedsDisplay];
    
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
    _placeholderColor = placeholderColor;
    [self setNeedsDisplay];
}

- (void)setText:(NSString *)text
{
    [super setText:text];
    [self setNeedsDisplay];
}


@end
